from 期末项目.User.User import User


class ManagerSystem:
    def __init__(self):
        self.users = []

    def run(self):
        while True:
            print("1. 添加用户")
            print("2. 删除用户")
            print("3. 修改用户")
            print("4. 查询用户")
            print("5. 查询全部用户")
            print("0. 退出系统")
            choice = input("请输入操作编号：")

            if choice == "1":
                self.add_user()
            elif choice == "2":
                self.delete_user()
            elif choice == "3":
                self.modify_user()
            elif choice == "4":
                self.query_user()
            elif choice == "5":
                self.query_all_users()
            elif choice == "0":
                break
            else:
                print("无效的操作编号，请重新输入")

    def add_user(self):
        id = input("请输入用户编号：")
        username = input("请输入用户名：")
        password = input("请输入密码：")
        user = User(id, username, password)
        self.users.append(user)
        print("用户添加成功")

    def delete_user(self):
        id = input("请输入要删除的用户编号：")
        for user in self.users:
            if user.id == id:
                self.users.remove(user)
                print("用户删除成功")
                return
        print("未找到对应的用户")

    def modify_user(self):
        id = input("请输入要修改的用户编号：")
        for user in self.users:
            if user.id == id:
                username = input("请输入新的用户名：")
                password = input("请输入新的密码：")
                user.username = username
                user.password = password
                print("用户修改成功")
                return
        print("未找到对应的用户")

    def query_user(self):
        id = input("请输入要查询的用户编号：")
        for user in self.users:
            if user.id == id:
                print(user)
                return
        print("未找到对应的用户")

    def query_all_users(self):
        if len(self.users) == 0:
            print("暂无用户")
        else:
            for user in self.users:
                print(user)